﻿/*
    Inationsoft.Dictionary; Word Definition Class.
    Copyright (C) 2009  Inationsoft (Inanta Martsanto, inanta@inationsoft.co.cc)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.Linq;
using System.Xml.Linq;

namespace Inationsoft.Dictionary
{
    public class WordDefinition
    {
        private string _path, _group;
        private string _word, _read, _wordClass, _definition;

        public string Word
        {
            get
            {
                if (_word == null)
                {
                    XDocument _wdef = XDocument.Load(@_path);

                    var word = from item in _wdef.Descendants("word")
                               select item;

                    _word = word.FirstOrDefault().Value;
                }

                return _word;
            }
        }

        public string CleanWord
        {
            get
            {
                return Word.Replace("·", ""); ;
            }
        }

        public string WordClass
        {
            get
            {
                if (_wordClass == null)
                {
                    XDocument _wdef = XDocument.Load(@_path);

                    var wordClass = from item in _wdef.Descendants("word")
                                    select item;

                    _wordClass = getWordClass(wordClass.FirstOrDefault().Attribute("class").Value.ToString());
                }

                return _wordClass;
            }
        }

        public string Definition
        {
            get
            {
                if (_definition == null)
                {
                    XDocument _wdef = XDocument.Load(@_path);

                    var query = from definition in _wdef.Descendants("definition")
                                select definition;

                    _definition = query.FirstOrDefault().Value;
                }

                return _definition;
            }
        }

        public string ShortDefinition
        {
            get
            {
                if (Definition.Length > 200)
                {
                    return Definition.Replace("<i>", "").Replace("</i>", "").Substring(0, 200) + "...";
                }

                return Definition.Replace("<i>", "").Replace("</i>", "");
            }
        }

        public string Read
        {
            get
            {
                if (_read == null)
                {
                    XDocument _wdef = XDocument.Load(@_path);

                    var read = from item in _wdef.Descendants("read")
                               select item;

                    if (read.Count() == 0)
                    {
                        _read = "";
                    }
                    else
                    {
                        _read = read.FirstOrDefault().Value;
                    }

                }

                return _read;
            }
        }

        public string WordAndRead
        {
            get
            {
                if (Read != "")
                {
                    return Word + " /" + Read + "/";
                }

                return Word;
            }
        }

        public string HTMLWordDefinition
        {
            get
            {
                string retHTML = "<body style=\"color:#444444;\">";

                try
                {
                    retHTML +=
                    "<span style=\"font-size:18px; margin-bottom:10px; font-weight:bold;\">" + WordAndRead.Replace("·", "&#183").Replace("é", "&#233;") + "</span><br />"
                    + "<span style=\"margin-bottom:10px\"><i>" + WordClass + "</i></span><br />";

                    XDocument _wdef = XDocument.Load(@_path);

                    var mdefinitions = from item in _wdef.Descendants("definition")
                                       where item.Parent.Name == "wdef"
                                       select item;

                    if (mdefinitions.Count() == 1)
                    {
                        retHTML += "<div style=\"text-align:justify; font-family: Sans-Serif\">" + mdefinitions.FirstOrDefault().Value + "</div>";
                    }
                    else
                    {
                        for (int i = 0; i < mdefinitions.Count(); i++)
                        {
                            retHTML += "<div style=\"text-align:justify; font-family: Sans-Serif\"><b>" + (i + 1) + "</b> " + mdefinitions.ElementAt(i).Value + "</div>";
                        }
                    }

                    var subs = from item in _wdef.Descendants("sub")
                               select item;

                    if (subs.Count() > 0)
                    {
                        retHTML += "<div style=\"font-size:18px; margin-bottom:10px; margin-top:10px\"><i>" + "SUBLEMA" + "</i></div>";

                        foreach (var sub in subs)
                        {
                            var words = from item in sub.Descendants("word")
                                        select item;

                            var definitions = from item in sub.Descendants("definition")
                                              select item;

                            for (int i = 0; i < words.Count(); i++)
                            {
                                string wordClass = null;

                                try
                                {
                                    wordClass = words.ElementAt(i).Attribute("class").Value;

                                    if (wordClass == "inherit")
                                    {
                                        wordClass = WordClass;
                                    }
                                }
                                catch (System.Exception)
                                {
                                    wordClass = WordClass;
                                }

                                retHTML += "<span style=\"font-size:18px; margin-bottom:10px; font-weight:bold;\">" + words.ElementAt(i).Value.Replace("--", Word).Replace("·", "") + "</span><br />";
                                retHTML += "<span style=\"margin-bottom:10px\"><i>" + wordClass + "</i></span><br />";
                                retHTML += "<div style=\"text-align:justify; font-family: Sans-Serif\">" + definitions.ElementAt(i).Value + "<br /><br /></div>";
                            }
                        }
                    }

                    var derivatives = from item in _wdef.Descendants("derivative")
                                      select item;

                    if (derivatives.Count() > 0)
                    {
                        retHTML += "<div style=\"font-size:18px; margin-bottom:10px; margin-top:10px\"><i>" + "KATA TURUNAN" + "</i></div>";

                        foreach (var derivative in derivatives)
                        {
                            var words = from item in derivative.Descendants("word")
                                        select item;

                            var definitions = from item in derivative.Descendants("definition")
                                              select item;

                            for (int i = 0; i < words.Count(); i++)
                            {
                                string wordClass = words.ElementAt(i).Attribute("class").Value;

                                retHTML += "<span style=\"font-size:18px; margin-bottom:10px; font-weight:bold;\">" + words.ElementAt(i).Value.Replace("·", "&#183;") + "</span><br />";
                                retHTML += "<span style=\"margin-bottom:10px\"><i>" + getWordClass(wordClass) + "</i></span><br />";
                                retHTML += "<div style=\"text-align:justify; font-family: Sans-Serif\">" + definitions.ElementAt(i).Value + "<br /><br /></div>";
                            }
                        }
                    }

                    retHTML += "</body>";
                }
                catch (System.Exception)
                { }

                return retHTML;
            }
        }

        public WordDefinition(string path)
        {
            _path = path;
        }

        public WordDefinition(Word word)
        {
            _path = @"References\" + word.Group + @"\" + word.Name;
            _group = word.Group;
        }

        private string getWordClass(string wordClass)
        {
            if (WindowMain.WordClass.ContainsKey(wordClass))
            {
                return WindowMain.WordClass[wordClass];
            }

            return wordClass;
        }
    }
}
